package com.kakarote.examine.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.kakarote.core.common.enums.ExamineLabelEnum;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.feign.crm.entity.ExamineField;
import com.kakarote.core.feign.examine.entity.ExamineFlowConditionDataVO;
import com.kakarote.core.feign.hrm.HrmFeignService;
import com.kakarote.examine.common.LanguageFieldUtil;
import com.kakarote.examine.constant.ExamineCodeEnum;
import com.kakarote.examine.constant.ExamineConst;
import com.kakarote.examine.constant.ExamineStatusEnum;
import com.kakarote.examine.service.ExamineModuleService;
import com.kakarote.examine.service.IExamineService;
import com.kakarote.hrm.feign.entity.HrmEmployee;
import com.kakarote.hrm.feign.entity.HrmSalaryMonthRecord;
import com.kakarote.hrm.feign.service.SalaryRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author JiaS
 * @date 2020/12/25
 */
@Service("examineHrmService")
public class ExamineHrmServiceImpl implements ExamineModuleService {

    @Resource SalaryRecordService salaryRecordService;
    @Resource HrmFeignService hrmFeignService;
    @Resource IExamineService examineService;

    @Override
    public List<ExamineField> queryExamineField(Integer label, Long categoryId) {
        //年月 实发工资
        List<ExamineField> examineFields = new ArrayList<>();
        examineFields.add(new ExamineField((long) -1, "计薪年", "year", 5, 1));
        examineFields.add(new ExamineField((long) -1, "计薪月", "month", 5, 1));
        examineFields.add(new ExamineField((long) -1, "实发工资", "realPaySalary", 6, 1));
        if (CollectionUtil.isNotEmpty(examineFields)) {
            for (ExamineField examineField : examineFields) {
                Map<String, String> keyMap = LanguageFieldUtil.getFieldNameKeyMap("name_resourceKey", "defaultParams.crmExamine" + label + ".", examineField.getFieldName(), examineField.getSetting());
                Map<String, String> remarksKey = LanguageFieldUtil.getFieldNameKeyMap("remarks_resourceKey", "defaultParams.crmExamineRemarks" + label + ".", examineField.getFieldName());
                keyMap.putAll(remarksKey);
                examineField.setLanguageKeyMap(keyMap);
            }
        }
        return examineFields;
    }

    /**
     * 更新业务状态
     */
    @Override
    public void updateCheckStatus(Integer label, Long typeId, Integer checkStatus, Object data) {
        if(ExamineLabelEnum.HRM_薪资.getCode()==label){
            salaryRecordService.updateCheckStatus(typeId, checkStatus);
        }else if(ExamineLabelEnum.HRM_入职流程.getCode()==label){
            hrmFeignService.finishEntryExamine(typeId, checkStatus);
        }else if(ExamineLabelEnum.HRM_离职流程.getCode()==label){
            hrmFeignService.finishLeaveExamine(typeId, checkStatus);
        }else if(ExamineLabelEnum.HRM_跨单位人员调动.getCode()==label ||ExamineLabelEnum.HRM_内部人员调动.getCode()==label ){
            hrmFeignService.finishEmployeeChange(typeId, checkStatus);
        }else if(ExamineLabelEnum.HRM_跨单位人员调动_调入.getCode()==label||ExamineLabelEnum.HRM_跨单位人员调动_调出.getCode()==label){
            hrmFeignService.finishChildrenFlow(typeId, checkStatus);
        }else if(ExamineLabelEnum.HRM_下级入职_集团审批.getCode()==label){
            hrmFeignService.finishGroupEntryExamine(typeId, checkStatus);
        }else if(ExamineLabelEnum.HRM_员工信息修改审批.getCode()==label){
            hrmFeignService.finishEmployeeInfoChangeExamine(typeId, checkStatus);
        }else if(ExamineLabelEnum.HRM_员工关键字段修改审批.getCode()==label){
            hrmFeignService.finishEmployeeKeyFieldChangeExamine(typeId, checkStatus);
        }else if(ExamineLabelEnum.HRM_员工职位层级调整审批.getCode()==label){
            hrmFeignService.finishEmployeePostLevelChangeExamine(typeId, checkStatus);
        }
    }


    @Override
    public void checkStatus(Integer label, Long typeId, Integer checkStatus, Integer oldCheckStatus) {
        int four = 4;
        if (checkStatus == four) {
            //当前审核已通过不可撤回
            if (oldCheckStatus == 1) {
                throw new CrmException(ExamineCodeEnum.EXAMINE_RECHECK_PASS_ERROR);
            }
        }
    }

    @Override
    public Map<String, Object> getConditionMap(Integer label, Long typeId, Long recordId) {
        Map<String, Object> map = new HashMap<>(4);
        List<String> fieldList = new ArrayList<>();
        List<ExamineFlowConditionDataVO> conditionDataVoS = examineService.previewFiledName(label, recordId, null);
        if (conditionDataVoS != null) {
            fieldList = conditionDataVoS.stream().map(ExamineFlowConditionDataVO::getFieldName).collect(Collectors.toList());
            fieldList.removeIf(StrUtil::isEmpty);
        }
        if (CollUtil.isEmpty(fieldList)) {
            return map;
        }
        HrmSalaryMonthRecord hrmSalaryMonthRecord = salaryRecordService.querySalaryRecordById(typeId).getData();
        Map<String, Object> beanMap = BeanUtil.beanToMap(hrmSalaryMonthRecord);
        fieldList.forEach(field -> map.put(field, beanMap.get(field)));
        map.put(ExamineConst.CREATE_USER_ID, hrmSalaryMonthRecord.getCreateUserId());
        return map;
    }

    /**
     * 获取成员组件需要的数据entity
     *
     * @param fieldName
     * @param label
     * @param typeId
     * @param recordId
     * @return
     */
    @Override
    public Map<String, Object> getMemberComponentMap(String fieldName, Integer label, Long typeId, Long recordId) {
        HrmSalaryMonthRecord hrmSalaryMonthRecord = salaryRecordService.querySalaryRecordById(typeId).getData();
        Map<String, Object> beanMap = BeanUtil.beanToMap(hrmSalaryMonthRecord);

        Map<String, Object> map = new HashMap<>(4);

        map.put(fieldName, beanMap.get(fieldName));

        return map;
    }
}
